import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/filter_components/mixin/advanced_filter_input_mixin.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';

/// author：william on 2021/4/15 09:26
/// email：362661044@qq.com

class AdvancedFilterInputText extends StatefulWidget {
  final BaseAdvancedFilterModel model;

  const AdvancedFilterInputText({Key key, this.model}) : super(key: key);

  @override
  _AdvancedFilterInputTextState createState() => _AdvancedFilterInputTextState();
}

class _AdvancedFilterInputTextState extends State<AdvancedFilterInputText> with AdvancedFilterInputMixin {
  String textValue = '';

  @override
  BaseAdvancedFilterModel get model => widget.model;

  @override
  void initStateEvent() {
    var data = baseRequestFormProvider.getDataKey(model);
    if (data != null) {
      if (data == -2) isAll = true;

      if (data is String) textValue = data;
    }
  }

  @override
  Widget buildContent() {
    return buildTextContainer(textValue);
  }

  @override
  void resetEvent() {
    isAll = false;
    textValue = '';
  }

  @override
  void showDialog() {
    showDialogEvent(
      title: model.name ?? '',
      child: BaseTextField(
        inputBorder: inputBorder,
        value: textValue,
        onChanged: (value) => {textValue = value},
      ),
      onOKPress: () {
        if (textValue.isEmpty) return BaseUtils.showToast('${model.name}不能为空');
        isAll = false;
        baseRequestFormProvider.setDataSource(model, textValue);
        setState(() {});
        Navigator.pop(context);
      },
    );
  }
}
